using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.CMS.Library;
using NCS.Library.Utilities;
using NCS.Library;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class TinAnh : CMSWebControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable tbl_zones = NCS.Module.CMS.Library.Zones.ZoneController.GetZonesVisbleInTop();
            this.rpt_zone.DataSource = tbl_zones;
            this.rpt_zone.ItemDataBound += new RepeaterItemEventHandler(rpt_zone_ItemDataBound);
            this.rpt_zone.DataBind();
        }

        void rpt_zone_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null) {

                HyperLink link_zone = (HyperLink)e.Item.FindControl("link_zone");

                int zone_authorize = ConvertUtility.ToInt32(row["Zone_Authorize"].ToString(), 0);
                if (zone_authorize > 0)
                {
                    if (!this.Request.IsAuthenticated)
                    {
                        e.Item.Visible = false;
                        return;
                    }
                    else
                    {

                        if (this.CurrentUser.UserType == 0 && zone_authorize > 1)
                        {
                            e.Item.Visible = false;
                            return;
                        }
                    }
                }

                int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_ID"].ToString());
                int tabid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Portal_TabControlID"].ToString());
                int authorize = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_Authorize"], 0);
                if (zoneid != 0 && ((authorize == 0 && this.Request.IsAuthenticated == false) || (authorize <= 1 && this.CurrentUser.UserType == 0) || (authorize <= 2 && this.CurrentUser.UserType == 1)))
                {
                    string url = string.Empty;// BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());
                    if (row["Zone_Url"] != null)
                        url = row["Zone_Url"].ToString();
                    if (url.Length == 0)
                        url = BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());

                    link_zone.NavigateUrl = url;
                    link_zone.Text = row["Zone_Name"].ToString();

                    DataTable tbl_news = CMS.Library.Distributions.DistributionController.GetDistributedContents(zoneid, 2, null);
                    Repeater rpt_news = (Repeater)e.Item.FindControl("rpt_news");
                    rpt_news.DataSource = tbl_news;
                    rpt_news.ItemDataBound += new RepeaterItemEventHandler(rpt_news_ItemDataBound);
                    rpt_news.DataBind();
                }
                else
                {
                    e.Item.Visible = false;
                    return;
                }
            }
        }

        void rpt_news_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                HyperLink link = (HyperLink)e.Item.FindControl("link_news");

                link.Text = ConvertUtility.ToString(row["Content_Headline"]);
                int zoneid = ConvertUtility.ToInt32(row["Distribution_ZoneID"]);
                link.NavigateUrl = BuildUrlByDistId((int)row["Content_ID"], zoneid);
                Image img = (Image)e.Item.FindControl("img_news");
                if (!string.IsNullOrEmpty(row["Content_Avatar"].ToString()))
                {
                    img.ImageUrl = MultimediaUtility.GetListAvatar(row["Content_Avatar"].ToString());
                }
                else {
                    img.Visible = false;
                }
            }
        }
    }
}